﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

using DotFuzzy;

namespace Fuzzy
{
	public class Win32
	{
		[DllImport( "dotfuzzy.dll" )]
		public static extern void SetWindowText( int h, String s );
	}

	public partial class Form1: Form
	{
		public Form1()
		{
			InitializeComponent();

			LinguisticVariable water = new LinguisticVariable( "Water" );
			water.MembershipFunctionCollection.Add( new MembershipFunction( "Cold", 0, 0, 20, 40 ) );
			water.MembershipFunctionCollection.Add( new MembershipFunction( "Tepid", 30, 50, 50, 70 ) );
			water.MembershipFunctionCollection.Add( new MembershipFunction( "Hot", 50, 80, 100, 100 ) );

			LinguisticVariable power = new LinguisticVariable( "Power" );
			power.MembershipFunctionCollection.Add( new MembershipFunction( "Low", 0, 25, 25, 50 ) );
			power.MembershipFunctionCollection.Add( new MembershipFunction( "High", 25, 50, 50, 75 ) );

			LinguisticVariable cycle = new LinguisticVariable( "Cycle" );
			cycle.MembershipFunctionCollection.Add( new MembershipFunction( "Short", 0, 10, 10, 30 ) );
			cycle.MembershipFunctionCollection.Add( new MembershipFunction( "Long", 10, 30, 30, 100 ) );

			FuzzyEngine fuzzyEngine = new FuzzyEngine();

			// Fuzzy Input
			fuzzyEngine.LinguisticVariableCollection.Add( water );
			fuzzyEngine.LinguisticVariableCollection.Add( power );

			// Fuzzy Output
			fuzzyEngine.LinguisticVariableCollection.Add( cycle );

			// Result Variable
			fuzzyEngine.Consequent = "Cycle";

			// Fuzzy Rules
			fuzzyEngine.FuzzyRuleCollection.Add( new FuzzyRule( "IF (Water IS Cold) AND (Power IS Low) THEN Cycle IS Short" ) );

			
			// Input Variable
			water.InputValue = 20;
			power.InputValue = 10;

			try
			{
				MessageBox.Show( fuzzyEngine.Defuzzify().ToString() );
			}
			catch ( Exception e )
			{
				MessageBox.Show( e.Message );
			}


		}
	}
}
